package com.zzy.lang.extend;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * @author guokaige
 *
 */
public final class StringUtils {

	public static final String COMMA = ",";
	public static final String VERTICAL = "|";
	public static final String INTERNET_PREFIX_HTTP = "http://";
	public static final String INTERNET_PREFIX_HTTPS = "https://";

	public static final String FOLDER_SEPARATOR = "/";

	public static final String WINDOWS_FOLDER_SEPARATOR = "\\";

	public static final String TOP_PATH = "..";

	public static final String CURRENT_PATH = ".";

	public static final char EXTENSION_SEPARATOR = '.';

	public static final String UNDERLINE = "_";
	
	/**
	 * Check whether the given CharSequence has actual text.
	 */
	public static boolean hasText(CharSequence str) {
		if (!hasLength(str)) {
			return false;
		}
		int strLen = str.length();
		for (int i = 0; i < strLen; i++) {
			if (!Character.isWhitespace(str.charAt(i))) {  //空白字段
				return true;
			}
		}
		return false;
	}
	
//	java.lang.Character.isWhitespace(char ch) 确定指定的字符是否为Java空白。字符是一个Java空白字符，当且仅当它满足下列条件之一：
//
//	它是一个Unicode字符空格 (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) 但不也是非间断空格('u00A0', 'u2007', 'u202F').
//
//	它是 '	', U+0009 HORIZONTAL TABULATION.
//
//	它是 ' ', U+000A LINE FEED.
//
//	它是 'u000B', U+000B VERTICAL TABULATION.
//
//	它是 'f', U+000C FORM FEED.
//
//	它是 ' ', U+000D CARRIAGE RETURN.
//
//	它是 'u001C', U+001C FILE SEPARATOR.
//
//	它是 'u001D', U+001D GROUP SEPARATOR.
//
//	它是 'u001E', U+001E RECORD SEPARATOR.
//
//	它是 'u001F', U+001F UNIT SEPARATOR.
	

	/**
	 * 字符(不包含空格字符串)不为空
	 * @Title:        title
	 * @Description:  TODO(这里用一句话描述这个方法的作用) 
	 * @param str
	 * @return    
	 * @version       V3.0 
	 * @author        guokaige@topnet.net.cn
	 * @Date          2016年11月7日 下午1:18:55
	 */
	public static boolean hasText(String str) {
		return hasText((CharSequence) str);
	}
	
	
	
	
	//用正则表达式
	//首先要import java.util.regex.Pattern 和 java.util.regex.Matcher
	public static boolean isNumeric(String str){ 
	   Pattern pattern = Pattern.compile("[-]?[0-9]*"); 
	   Matcher isNum = pattern.matcher(str);
	   if( !isNum.matches() ){
	       return false; 
	   } 
	   return true; 
	}
	
	
	
	
	public static void main(String[] args) {
		System.out.println(StringUtils.isNumeric("-90"));
	}
	
	
	
	
	
	

	/**
	 * 包含空白字段
	 * @Title:        title
	 * @Description:  TODO(这里用一句话描述这个方法的作用) 
	 * @param str
	 * @return    
	 * @version       V3.0 
	 * @author        guokaige@topnet.net.cn
	 * @Date          2016年11月7日 下午1:12:04
	 */
	public static boolean containsWhitespace(CharSequence str) {
		if (!hasLength(str)) {
			return false;
		}
		int strLen = str.length();
		for (int i = 0; i < strLen; i++) {
			if (Character.isWhitespace(str.charAt(i))) {
				return true;
			}
		}
		return false;
	}
	
	

	/**
	 * 字符(包含空格字符串)长度大于0              
	 * @Title:        title
	 * @Description:  TODO(这里用一句话描述这个方法的作用) 
	 * @param str
	 * @return    
	 * @version       V3.0 
	 * @author        guokaige@topnet.net.cn
	 * @Date          2016年11月7日 下午1:17:50
	 */
	public static boolean hasLength(CharSequence str) {
		return (str != null && str.length() > 0);
	}



	/**
	 * 包含空白字段
	 * @Title:        title
	 * @Description:  TODO(这里用一句话描述这个方法的作用) 
	 * @param str
	 * @return    
	 * @version       V3.0 
	 * @author        guokaige@topnet.net.cn
	 * @Date          2016年11月7日 下午1:12:04
	 */
	public static boolean containsWhitespace(String str) {
		return containsWhitespace((CharSequence) str);
	}

	/**
	 * 去除空白字段
	 * @Title:        title
	 * @Description:  TODO(这里用一句话描述这个方法的作用) 
	 * @param str
	 * @return    
	 * @version       V3.0 
	 * @author        guokaige@topnet.net.cn
	 * @Date          2016年11月7日 下午1:12:04
	 */
	public static String trimAllWhitespace(String str) {
		if (!hasLength(str)) {
			return str;
		}
		StringBuffer buf = new StringBuffer(str);
		int index = 0;
		while (buf.length() > index) {
			if (Character.isWhitespace(buf.charAt(index))) {
				buf.deleteCharAt(index);
			} else {
				index++;
			}
		}
		return buf.toString();
	}

	
	
	
	
	/**
	 * Trim leading whitespace from the given String.
	 */
	public static String trimLeadingWhitespace(String str) {
		if (!hasLength(str)) {
			return str;
		}
		StringBuffer buf = new StringBuffer(str);
		while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) {
			buf.deleteCharAt(0);
		}
		return buf.toString();
	}

	/**
	 * Trim trailing whitespace from the given String.
	 */
	public static String trimTrailingWhitespace(String str) {
		if (!hasLength(str)) {
			return str;
		}
		StringBuffer buf = new StringBuffer(str);
		while (buf.length() > 0 && Character.isWhitespace(buf.charAt(buf.length() - 1))) {
			buf.deleteCharAt(buf.length() - 1);
		}
		return buf.toString();
	}

	/**
	 * Trim all occurences of the supplied leading character from the given
	 * String.
	 */
	public static String trimLeadingCharacter(String str, char leadingCharacter) {
		if (!hasLength(str)) {
			return str;
		}
		StringBuffer buf = new StringBuffer(str);
		while (buf.length() > 0 && buf.charAt(0) == leadingCharacter) {
			buf.deleteCharAt(0);
		}
		return buf.toString();
	}

	/**
	 * Trim all occurences of the supplied trailing character from the given
	 * String.
	 */
	public static String trimTrailingCharacter(String str,char trailingCharacter) {
		if (!hasLength(str)) {
			return str;
		}
		StringBuffer buf = new StringBuffer(str);
		while (buf.length() > 0
				&& buf.charAt(buf.length() - 1) == trailingCharacter) {
			buf.deleteCharAt(buf.length() - 1);
		}
		return buf.toString();
	}

	/**
	 * Test if the given String starts with the specified prefix, ignoring
	 * upper/lower case.
	 */
	public static boolean startsWithIgnoreCase(String str, String prefix) {
		if (str == null || prefix == null) {
			return false;
		}
		if (str.startsWith(prefix)) {
			return true;
		}
		if (str.length() < prefix.length()) {
			return false;
		}
		String lcStr = str.substring(0, prefix.length()).toLowerCase();
		String lcPrefix = prefix.toLowerCase();
		return lcStr.equals(lcPrefix);
	}

	/**
	 * Test if the given String ends with the specified suffix, ignoring
	 * upper/lower case.
	 */
	public static boolean endsWithIgnoreCase(String str, String suffix) {
		if (str == null || suffix == null) {
			return false;
		}
		if (str.endsWith(suffix)) {
			return true;
		}
		if (str.length() < suffix.length()) {
			return false;
		}

		String lcStr = str.substring(str.length() - suffix.length())
				.toLowerCase();
		String lcSuffix = suffix.toLowerCase();
		return lcStr.equals(lcSuffix);
	}

	/**
	 * Test whether the given string matches the given substring at the given
	 * index.
	 */
	public static boolean substringMatch(CharSequence str, int index,
			CharSequence substring) {
		for (int j = 0; j < substring.length(); j++) {
			int i = index + j;
			if (i >= str.length() || str.charAt(i) != substring.charAt(j)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Count the occurrences of the substring in string s
	 */
	public static int countOccurrencesOf(String str, String sub) {
		if (str == null || sub == null || str.length() == 0
				|| sub.length() == 0) {
			return 0;
		}
		int count = 0, pos = 0, idx = 0;
		while ((idx = str.indexOf(sub, pos)) != -1) {
			++count;
			pos = idx + sub.length();
		}
		return count;
	}

	/**
	 * 
	 * from array to delimited String.
	 */
	public static String arrayByDelimitedString(Object[] arr, String delim) {
		if (ObjectUtils.isEmpty(arr)) {
			return "";
		}
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < arr.length; i++) {
			if (i > 0) {
				sb.append(delim);
			}
			sb.append(arr[i]);
		}
		return sb.toString();
	}


	

	/**
	 * 
	 * StringToLongArray(such as string "1,2,3,4" convert to LongArray")
	 * 
	 * TODO(Here describes this method to be suitable the condition - to be
	 * possible to elect)
	 * 
	 * @param str
	 * @return
	 * 
	 *         Long[]
	 */
	public static Long[] StringToLongArray(String str) {
		if (str != null && str.length() > 0) {
			String[] targetIdsArray = str.split(StringUtils.COMMA);
			int arrayLength = targetIdsArray.length;
			Long[] targetIdsLongArray = new Long[arrayLength];
			for (int i = 0; i < arrayLength; i++) {
				targetIdsLongArray[i] = Long.parseLong(targetIdsArray[i]);
			}
			return targetIdsLongArray;
		} else {
			return null;
		}
	}
	
	

}
